import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Queue;

public class ScoreOfParentheses {

    public static Integer solution(String s){
        Deque<Integer> queue=new ArrayDeque<>();
        queue.addLast(0);
        for(char c:s.toCharArray()){
            if(c=='('){
                queue.addLast(0);
            }else{
                Integer cur=queue.pollLast();
                queue.addLast(queue.pollLast()+Math.max(cur*2,1));
            }
        }
        return queue.peekLast();
    }

    public static void main(String[] args) {
        System.out.println(solution("(()(()))"));
    }
}
